package com.qinge.blog.utils.mybatis;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * @author QDW
 * @date 2022/5/9 18:40
 * @description IPage工具类
 **/
public class IPageUtil {

    /**
     * @author QDW
     * @date 2022/5/9 18:48
     * @description list转Page
     * @param page
     * @param list
     * @return com.baomidou.mybatisplus.core.metadata.IPage<T>
     **/
    public static <T> IPage<T> listToPage( Page<T> page,List<T> list){
        List<T> pageList = new ArrayList<>();
        int pageNum= (int) page.getCurrent();
        int pageSize= (int) page.getSize();

        int curIdx = pageNum > 1 ? (pageNum - 1) * pageSize : 0;
        for (int i = 0; i < pageSize && curIdx + i < list.size(); i++) {
            pageList.add(list.get(curIdx + i));
        }

        page.setRecords(pageList);
        page.setTotal(list.size());
        return page;
    }



    public static <T> IPage<T> listToPage( int pageNum, int pageSize,List<T> list){
        List<T> pageList = new ArrayList<>();
        int curIdx = pageNum > 1 ? (pageNum - 1) * pageSize : 0;
        for (int i = 0; i < pageSize && curIdx + i < list.size(); i++) {
            pageList.add(list.get(curIdx + i));
        }
        IPage<T> page = new Page<>(pageNum, pageSize);
        page.setRecords(pageList);
        page.setTotal(list.size());
        return page;
    }
}
